﻿using Domain.Config;
using Domain.IRepositorys;
using Domain.Model;
using Infrastructure.UniOfWorkBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Infrastructure;
using Infrastructure.Cache;

namespace Application.Config
{
    public class ConfigApplication
    {
        public IEFUnitOfWork EFUnitOfWork { get; set; }
        public IDicRepository dicRepository { get; set; }

        public void LoadSystemSetting()
        {
            var scs = dicRepository.Entities.Where(i => i.GroupName == "系统配置").ToArray();
            var bcs = dicRepository.Entities.Where(i => i.GroupName == "基础配置").ToArray();
            var sts = dicRepository.Entities.Where(i => i.GroupName == "超级商人").ToArray();
            var mcs = dicRepository.Entities.Where(i => i.GroupName == "任务达人").ToArray();

            foreach (var item in bcs)
            {
                SystemConfig.SetConfigName(item.GroupName, item.KeyName, item.KeyValue);
            }
            foreach (var item in sts)
            {
                SystemConfig.SetConfigName(item.GroupName, item.KeyName, item.KeyValue);
            }
            foreach (var item in mcs)
            {
                SystemConfig.SetConfigName(item.GroupName, item.KeyName, item.KeyValue);
            }
            EFUnitOfWork.IsCommitted = true;
        }
        public void InitConfig()
        {
            var systemConfigs = SystemConfig.GetConfigListByName("系统配置");
            var baseConfigs = SystemConfig.GetConfigListByName("基础配置");
            var superTraderConfigs = SystemConfig.GetConfigListByName("超级商人");
            var masterConfigs = SystemConfig.GetConfigListByName("任务达人");

            var scs = dicRepository.Entities.Where(i => i.GroupName == "系统配置");
            var bcs = dicRepository.Entities.Where(i => i.GroupName == "基础配置");
            var sts = dicRepository.Entities.Where(i => i.GroupName == "超级商人");
            var mcs = dicRepository.Entities.Where(i => i.GroupName == "任务达人");

            var scExcepts = systemConfigs.Select(i => i.Item1).Except(scs.Select(i => i.KeyName));
            var bcExcepts = baseConfigs.Select(i => i.Item1).Except(bcs.Select(i => i.KeyName));
            var stExcepts = superTraderConfigs.Select(i => i.Item1).Except(sts.Select(i => i.KeyName));
            var mcExcepts = masterConfigs.Select(i => i.Item1).Except(mcs.Select(i => i.KeyName));

            foreach (var item in scExcepts)
            {
                var dic = new DicAggregate();
                dic.GroupName = "系统配置";
                dic.KeyName = item;
                dic.KeyValue = systemConfigs.First(i => i.Item1 == item)?.Item2;

                dicRepository.Insert(dic);
            }
            foreach (var item in bcExcepts)
            {
                var dic = new DicAggregate();
                dic.GroupName = "基础配置";
                dic.KeyName = item;
                dic.KeyValue = baseConfigs.First(i => i.Item1 == item)?.Item2;

                dicRepository.Insert(dic);
            }
            foreach (var item in stExcepts)
            {
                var dic = new DicAggregate();
                dic.GroupName = "超级商人";
                dic.KeyName = item;
                dic.KeyValue = superTraderConfigs.First(i => i.Item1 == item)?.Item2;

                dicRepository.Insert(dic);
            }
            foreach (var item in mcExcepts)
            {
                var dic = new DicAggregate();
                dic.GroupName = "任务达人";
                dic.KeyName = item;
                dic.KeyValue = masterConfigs.First(i => i.Item1 == item)?.Item2;

                dicRepository.Insert(dic);
            }

            EFUnitOfWork.Commit();


            LoadSystemSetting();
        }


    }
}
